package com.bcxk.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.bcxk.entity.dto.User;
import com.bcxk.mapper.UserMapper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * @author nmk
 */
@Service
public class UserService extends ServiceImpl<UserMapper , User> {
    @Transactional(readOnly = true)
    public User getUser(String userName){
        return this.baseMapper.checkUserName(userName);
    }

    @Transactional(readOnly = true)
    public User getUserByEmail(String email , String passWord){
        QueryWrapper<User> wrapper = new QueryWrapper<>();
        wrapper.lambda().eq(User::getEmail,email)
                .eq(User::getPassword,passWord);
        return this.getOne(wrapper);
    }


    @Transactional(readOnly = true)
    public User getName(String userName ){
        QueryWrapper<User> wrapper = new QueryWrapper<>();
        wrapper.lambda().eq(User::getName,userName);
        return this.getOne(wrapper);
    }
    //验证手机号 或者邮箱是否已被注册
    @Transactional(readOnly = true)
    public User checkUserName(String userName){
       return  this.baseMapper.checkUserName(userName);
    }
    //修改用户信息
    @Transactional
    public void  updateUser(User user){
        this.saveOrUpdate(user);
    }

    @Transactional(readOnly = true)
    public User getUserByPhone(String phone) {
        QueryWrapper<User> wrapper = new QueryWrapper<>();
        wrapper.lambda().eq(User::getPhone,phone);
        return this.getOne(wrapper);
    }
}
